package com.logictech.lims.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import org.jeecgframework.p3.core.utils.common.PageList;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.utils.common.Pagenation;

import com.logictech.lims.service.ExperimentMaterialService;
import com.logictech.lims.service.ExperimentService;
import com.logictech.lims.service.LogService;
import com.logictech.lims.entity.Experiment;
import com.logictech.lims.dao.ExperimentDao;
import com.logictech.lims.dao.ExperimentMaterialDao;
import com.logictech.lims.dto.ExperimentMaterialDto;

@Service("experimentService")
public class ExperimentServiceImpl implements ExperimentService {
	@Resource
	private ExperimentDao experimentDao;
	
	@Resource
	private ExperimentMaterialService experimentMaterialService;
	
	@Resource
	private LogService logService;

	@Override
	public void doAdd(Experiment experiment) {
		experimentDao.add(experiment);
		logService.doAdd("1",experiment);
	}

	@Override
	public void doEdit(Experiment experiment) {
		experimentDao.update(experiment);
		logService.doAdd("2",experiment);
	}

	@Override
	public void doDelete(String id) {
		Experiment experiment  = experimentDao.get(id);
		logService.doAdd("3",experiment);
		
		// 查询实验材料
		List<ExperimentMaterialDto> experimentMaterialDtoList = experimentMaterialService.queryByExperimentId(id);
		for(int i =0;i<experimentMaterialDtoList.size();i++){
			String experimentMaterialId = experimentMaterialDtoList.get(i).getId().toString();
			experimentMaterialService.doDelete(experimentMaterialId);
		}
		experimentDao.delete(id);
	}

	@Override
	public Experiment queryById(String id) {
		Experiment experiment  = experimentDao.get(id);
		return experiment;
	}

	@Override
	public PageList<Experiment> queryPageList(
		PageQuery<Experiment> pageQuery) {
		PageList<Experiment> result = new PageList<Experiment>();
		Integer itemCount = experimentDao.count(pageQuery);
		List<Experiment> list = experimentDao.queryPageList(pageQuery,itemCount);
		Pagenation pagenation = new Pagenation(pageQuery.getPageNo(), itemCount, pageQuery.getPageSize());
		result.setPagenation(pagenation);
		result.setValues(list);
		return result;
	}
	
}
